<?php
/**
 * Autoloader below is a quick way to just make this example run. On production code, it would be at another place 
 */
function __autoload($class_name) {
    include $class_name.'.php';
}

/**
 * Sample requirements for this example are:
 * 1. Handle some typical responses expected from some typical REST API implementations
 * 2. Demonstrate some interfaces & code design patterns as necessary
 * 3. Exception handling
 * 
 * Some sample JSON responses used for this test, that may come from lets say an Authentication API request are given below. Either of the two may be used.
 * The program can handle other multiple response types - XML, JSON, AMF etc. but this example works using JSON & code blocks for XML, AMF decoding are left empty.
 * Basic Exception handling for this example is done via a custom ExceptionHandler which can handle code generated exceptions & based on exception type, take actions like - log to file, send email/sms notifications etc.
 * There is a single point of entry into the application which makes things like exception handling, debugging etc. easier for this part of the system. Also this provides modularity as this code piece/module will be working with some other system parts as well of'course. 
 */
$json1 =<<<EOP
{"result_code":0,"data":{"authID":"0fc9922226a2fa2ae1dadf3154e1817d"}}
EOP;
$json2 =<<<EOP
{"result_code":-1,"error":"Invalid username or password"}
EOP;

try{
	/**
	 * As an example 'Factory' pattern in use here
	 * One of the primary benefits would be decoupling or prevent tight coupling of code that acts on the data & parts that actually do the decoding, validation, cleaning, preparation etc.
	 */
	$response = ResponseFactory::Create($json1);

	/**
	 * As another example, below we are using the 'Virtual Proxy' pattern
	 * As a benefit we can handle data regardless of the details of actual class implementations - see doSomeAction() method implementation for details
	 * Enter description here ...
	 * @var unknown_type
	 */
	$handleData = new HandleData();
	$handleData->doSomeAction($response);
}
catch(ExceptionHandler $e)
{
	/*
	 * can do something here, before general purpose exception handling - based on requirements on what todo with exception
	 */
	
	//pass it on to out custom exception handler
	throw new ExceptionHandler($e->getMessage(), $e->getCode());	//the handler does the rest
}
catch(Exception $e)
{
	/*
	 * can do something here, before general purpose exception handling - based on requirements on what todo with exception
	 */
	
	//pass it on to out custom exception handler
	throw new ExceptionHandler($e->getMessage(), $e->getCode());	//the handler does the rest
}
